package me.exchange.com.modules.system.service.impl;

import me.exchange.com.modules.system.domain.RolesMenus;
import me.exchange.com.modules.system.dto.RolesMenusDto;
import me.exchange.com.modules.system.dto.RolesMenusQueryCriteria;
import me.exchange.com.modules.system.mapper.RolesMenusMapper;
import me.exchange.com.modules.system.repository.RolesMenusRepository;
import me.exchange.com.modules.system.service.RolesMenusService;
import me.exchange.com.utils.FileUtil;
import me.exchange.com.utils.PageUtil;
import me.exchange.com.utils.QueryHelp;
import me.exchange.com.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author zjj
* @date 2020-05-06
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RolesMenusServiceImpl implements RolesMenusService {
    @Autowired
    private RolesMenusRepository rolesMenusRepository;
    @Autowired
    private RolesMenusMapper rolesMenusMapper;

    @Override
    public Map<String,Object> queryAll(RolesMenusQueryCriteria criteria, Pageable pageable){
        Page<RolesMenus> page = rolesMenusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rolesMenusMapper::toDto));
    }

    @Override
    public List<RolesMenusDto> queryAll(RolesMenusQueryCriteria criteria){
        Sort sort = criteria.getSort();
        return rolesMenusMapper.toDto(rolesMenusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort));
    }

    @Override
    public RolesMenusDto findById(Long roleId) {
        RolesMenus rolesMenus = rolesMenusRepository.findById(roleId).orElseGet(RolesMenus::new);
        ValidationUtil.isNull(rolesMenus.getRoleId(),"RolesMenus","roleId",roleId);
        return rolesMenusMapper.toDto(rolesMenus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RolesMenusDto create(RolesMenus resources) {
        return rolesMenusMapper.toDto(rolesMenusRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RolesMenus resources) {
        RolesMenus rolesMenus = rolesMenusRepository.findById(resources.getRoleId()).orElseGet(RolesMenus::new);
        ValidationUtil.isNull( rolesMenus.getRoleId(),"RolesMenus","id",resources.getRoleId());
        rolesMenus.copy(resources);

        rolesMenusRepository.save(rolesMenus);
    }

    @Override
    public void delete(Long id) {
        RolesMenus rolesMenus = rolesMenusRepository.findById(id).orElseGet(RolesMenus::new);
        rolesMenusRepository.save(rolesMenus);
    }
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            RolesMenus rolesMenus = rolesMenusRepository.findById(id).orElseGet(RolesMenus::new);
            rolesMenusRepository.save(rolesMenus);
        }
    }

    @Override
    public void download(List<RolesMenusDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RolesMenusDto rolesMenus : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}